import os
import logging as logging
from logging.handlers import TimedRotatingFileHandler
# from tools import singleton

# @singleton
class MyLogger(logging.Logger):
    def __init__(self, name):
        super().__init__(name)
        path = os.path.join("./log",name)
        if os.path.exists(path) is False:
            os.makedirs(path)
        self.logfilename = os.path.join(path,f"log.log")
        # 输出到控制台
        handler = logging.StreamHandler()
        formatter = logging.Formatter('[%(asctime)s][%(levelname)s][%(message)s]')
        handler.setFormatter(formatter)
        self.addHandler(handler)

        # 输出到日志文件
        handler = TimedRotatingFileHandler(self.logfilename, when='d', interval=1, backupCount=7, encoding='utf-8')
        formatter = logging.Formatter('[%(asctime)s][%(levelname)s]%(message)s')
        handler.setFormatter(formatter)
        self.addHandler(handler)
        self.debug('日志初始化')

    def __del__(self):
        self.handlers.clear()
        print('删除日志模块')
# mlog = MyLogger('x')

